package com.powernode.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.powernode.constant.BusinessEnum;
import com.powernode.domain.MemberAddr;
import com.powernode.domain.Order;
import com.powernode.domain.OrderItem;
import com.powernode.ex.headler.BusinessException;
import com.powernode.feign.OrderMemberFeign;
import com.powernode.mapper.OrderItemMapper;
import com.powernode.mapper.OrderMapper;

import com.powernode.model.Result;
import com.powernode.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderMemberFeign orderMemberFeign;

    @Override
    public Page<Order> queryOrderPage(Page<Order> orderPage, String orderNumber, Integer status, Date startTime, Date endTime) {
//        多条件分页查询订单列表
        orderPage = orderMapper.selectPage(orderPage, new LambdaQueryWrapper<Order>()
                .eq(ObjectUtil.isNotNull(status), Order::getStatus, status)
                .between(ObjectUtil.isAllNotEmpty(startTime,endTime), Order::getCreateTime, startTime, endTime)
                .eq(StringUtils.hasText(orderNumber), Order::getOrderNumber, orderNumber)
                .orderByDesc(Order::getCreateTime)
        );
        List<Order> orderList = orderPage.getRecords();
        if(CollectionUtils.isEmpty(orderList) || orderList.size() == 0) {
            return orderPage;
        }
//        从订单记录集合中获取订单编号集合
        List<String> orderNumberList = orderList.stream().map(Order::getOrderNumber).collect(Collectors.toList());
//        根据订单编号查询所有订单商品条目
        List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                .in(OrderItem::getOrderNumber, orderNumberList)
        );
        orderList.forEach(order -> {
//            过滤
            List<OrderItem> itemList = orderItemList.stream()
                    .filter(orderItem -> orderItem.getOrderNumber()
                            .equals(order.getOrderNumber()))
                    .collect(Collectors.toList());
            order.setOrderItems(itemList);
        });
        return orderPage;
    }

    @Override
    public Order queryOrderDetailByOrderNumber(Long orderNumber) {
        Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber,orderNumber)
        );
//        根据订单编号查询所有订单商品条目
        List<OrderItem> orderItems = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                .eq(OrderItem::getOrderNumber, orderNumber)
        );
        order.setOrderItems(orderItems);
//        从订单记录中获取订单收货地址标识
        Long addrOrderId = order.getAddrOrderId();
//        根据收货地址查询地址详情 openfeign远程调用
        Result<MemberAddr> result = orderMemberFeign.getMemberAddrById(addrOrderId);
        if(result.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
            throw new BusinessException("远程接口调用失败，根据收货地址标识查询收货地址详情");
        }
        MemberAddr memberAddr = result.getData();
        order.setUserAddrOrder(memberAddr);
//        根据会员openID查询昵称 远程接口调用
        String openId = order.getOpenId();
        Result<String> result1 = orderMemberFeign.getNickNameByOpenId(openId);
        if(result1.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
            throw new BusinessException("远程接口调用失败，根据会员openId查询会员昵称失败");
        }
        String nickName = result1.getData();
        order.setNickName(nickName);
        return order;
    }
}
